//
//  LoginTwoViewController.m
//  Application
//
//  Created by 薛小刚 on 2017/12/21.
//  Copyright © 2017年 薛小刚. All rights reserved.
//

#import "LoginTwoViewController.h"
#import "ForgetPassworldViewController.h"
#import "NSString+DES.h"
#import <UMPush/UMessage.h>
#import "B_PerfectUserMessageVC.h"

@interface LoginTwoViewController ()
/// 关闭按钮点击事件
- (IBAction)closeButtonClicked:(id)sender;
/// 登录按钮点击事件
- (IBAction)loginButtonClicked:(id)sender;
/// 密码的view
@property (weak, nonatomic) IBOutlet UIView *passWorldView;
/// 密码输入框
@property (weak, nonatomic) IBOutlet UITextField *passworldInputTextfiled;
/// 忘记密码的点击事件
- (IBAction)forgetPassworldButtonCicked:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;

@end

@implementation LoginTwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    isAnimation = NO;
    [self setUI];
    [self getImage];
    [self.view sendSubviewToBack:self.backgroundImageView];
}
- (void)getImage {
    [NetworkingManager getNetworkRequestWithUrlString:@"http://39.106.113.252/bgmimg.json" parameters:nil isCache:NO succeed:^(id data) {
        [self.backgroundImageView sd_setImageWithURL:[NSURL URLWithString:data[@"img"]]];
    } fail:^(NSError *error) {
        self.backgroundImageView.backgroundColor = [UIColor blackColor];
    }];
    
}


static bool isAnimation;
- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:isAnimation];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

/**
 设置UI
 */
- (void) setUI {
//    self.passWorldView.layer.masksToBounds      = YES;
//    self.passWorldView.layer.cornerRadius       = 4;
//    self.passWorldView.layer.borderColor        = xgLineColor.CGColor;
//    self.passWorldView.layer.borderWidth        = 1;
    
    self.view.backgroundColor = [UIColor blackColor];
    [self.passworldInputTextfiled setValue:UIColorFromRGB(0xafafaf) forKeyPath:@"_placeholderLabel.textColor"];
    
//    [self.loginButton setTitleColor:xgTextGlodColor forState:UIControlStateNormal];
//    self.loginButton.backgroundColor = xgBackBlackColor;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)closeButtonClicked:(id)sender {
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)loginButtonClicked:(id)sender {
    [self.view endEditing:YES];
    
    if (ISNULL(self.passworldInputTextfiled.text)) {
        [UIView xg_toast:@"请输入密码!"];
        return;
    }
    
    [NetworkingManager postNetworkRequestWithUrlString:@"/yearnfor/inter/login/login" parameters:@{@"str": [NSString getEncypt:@{@"XxwyzMobile" : getMobile(), @"XxwyzPassword" : [self.passworldInputTextfiled.text MD5], @"XxwyzdeviceNumber" : getDeviceNumber(), @"XxwyzCode" : @"158178", @"XxwyzType" : @"2"}]} isCache:NO succeed:^(id data) {
        if ([data[@"code"] intValue] == 10000) {
            
            setUserId([NSString stringWithFormat:@"%@", data[@"values"][@"id"]]);
            setToken([NSString stringWithFormat:@"%@", data[@"values"][@"token"]]);
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshUserMessage" object:nil];
            [UMessage addAlias:[NSString stringWithFormat:@"%@", data[@"values"][@"id"]] type:kUMessageAliasTypeQQ response:^(id responseObject, NSError *error) {
                HXLog(@"失败信息是      %@", error);
            }];
            
            [NetworkingManager getNetworkRequestWithUrlString:[NSString stringWithFormat:@"/yearnfor/inter/login/userIsFillInfo/%@", getUserId()] parameters:nil isCache:NO succeed:^(id data) {
                if ([data[@"code"] isEqualToString:@"2"]) {     // 信息不完善 完善信息
                    B_PerfectUserMessageVC * vc = [[B_PerfectUserMessageVC alloc] init];
                    
                    [self pushNextViewController:vc];
                } else {
                    [self.navigationController.viewControllers.firstObject dismissViewControllerAnimated:YES completion:nil];
                }
            } fail:^(NSError *error) {
                [KeyWindow makeToast:@"服务器异常"];
            }];
            
//            [self.navigationController.viewControllers.firstObject dismissViewControllerAnimated:YES completion:nil];
        } else {
            [KeyWindow makeToast:data[@"message"]];
        }
        
    } fail:^(NSError *error) {
        
    }];
}
- (IBAction)forgetPassworldButtonCicked:(id)sender {
    [self.view endEditing:YES];
    ForgetPassworldViewController * forget = [[ForgetPassworldViewController alloc] init];
    [self pushNextViewController:forget];
}


- (void)dealloc {
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
    [[SDImageCache sharedImageCache] clearMemory];
}

@end
